package com.meichis.mcsappframework.adapter.expandablelistview;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;

import com.meichis.mcsappframework.adapter.expandablelistview.base.GroupExpandable;
import com.meichis.mcsappframework.adapter.expandablelistview.base.ViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by pei on 2016/9/26.
 */

public abstract class GroupAdapter<C extends GroupExpandable> extends BaseExpandableListAdapter {

    private List<C> datas;
    private Context context;
    private int grouplayoutId, childlayoutId;
    private List<String> group;
    private Map<String, List<C>> map;

    public GroupAdapter(Context context, int grouplayoutId, int childlayoutId, List<C> datas) {
        this.context = context;
        this.datas = datas;
        this.grouplayoutId = grouplayoutId;
        this.childlayoutId = childlayoutId;

        group = new ArrayList<>();//存储group
        map = new HashMap<>();
        List<C> childs;
        for (C c : datas) {
            childs = map.get(c.getGroupName());
            if (childs == null) {
                childs = new ArrayList<>();
                group.add(c.getGroupName());
            }
            childs.add(c);
            map.put(c.getGroupName(), childs);
        }
    }

    @Override
    public int getGroupCount() {
        return group.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return map.get(group.get(groupPosition)).size();
    }

    @Override
    public String getGroup(int groupPosition) {
        return group.get(groupPosition);
    }

    @Override
    public C getChild(int groupPosition, int childPosition) {
        return map.get(getGroup(groupPosition)).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = ViewHolder.get(context, convertView, parent, grouplayoutId, groupPosition);
        convertGroup(viewHolder, groupPosition, getGroup(groupPosition), map.get(getGroup(groupPosition)), isExpanded); // G必须实现ExpandableGroup接口
        return viewHolder.getConvertView();
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = ViewHolder.get(context, convertView, parent, childlayoutId, groupPosition);
        convertChild(viewHolder, groupPosition, getGroup(groupPosition), childPosition, map.get(getGroup(groupPosition)).get(childPosition), isLastChild);
        return viewHolder.getConvertView();
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }

    @Override
    public void notifyDataSetChanged() {
        group.clear();
        map.clear();
        List<C> childs;
        for (C c : datas) {
            childs = map.get(c.getGroupName());
            if (childs == null) {
                childs = new ArrayList<>();
                group.add(c.getGroupName());
            }
            childs.add(c);
            map.put(c.getGroupName(), childs);
        }
        super.notifyDataSetChanged();
    }


    protected abstract void convertGroup(ViewHolder viewHolder, int groupPosition, String GroupName, List<C> childs, boolean isExpanded);

    protected abstract void convertChild(ViewHolder viewHolder, int groupPosition, String GroupName, int childPosition, C child, boolean isLastChild);
}

